import React, { Component } from 'react'
import api from '@MODULES/common/api'
import { Link, withRouter } from 'react-router-dom'
import config from '@UTILS/config'
import store from 'store'
import cookies from "js-cookie"
import { Toast } from 'antd-mobile'
class PayOrder extends Component {
    constructor(props) {
        super(props)
        this.state = {
            payHtml: ''
        }
    }
    componentDidMount() {
        if (this.props.refs) {
            this.props.refs(this)
        }
    }
    render() {
        return (
            <div>
                {
                    this.state.payHtml ? <div dangerouslySetInnerHTML={{ __html: this.state.payHtml }} /> : ''
                }
            </div>
        )
    }

    weChatOrderPay = (orderId, type, callBack) => {
        var that = this,
            successFun = function () { }

        if (type === 'recharge') {
            successFun = api.wechatRecharge
        } else {
            successFun = api.wechatPayH5Configs
        }
        successFun({ order_sn: orderId }, {
            success: (data) => {
                if (data.json) {
                    window.WeixinJSBridge.invoke(
                        'getBrandWCPayRequest', data.json,
                        function (res) {
                            if (res.err_msg == "get_brand_wcpay_request:ok") {
                                // 使用以上方式判断前端返回,微信团队郑重提示：
                                // res.err_msg将在用户支付成功后返回
                                // ok，但并不保证它绝对可靠。
                                Toast.success('支付成功', 1, () => {
                                    if (callBack) {
                                        callBack(true)
                                    }
                                    // that.props.history.push('/pay/success')
                                })
                                //TODO
                            } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                                // 支付取消
                                Toast.fail('取消支付');
                                // 启用支付
                            } else if (res.err_msg == "get_brand_wcpay_request:fail") {
                                // 支付失败
                            }
                        }
                    );
                } else {
                    // 没有微信openid 去授权
                    let wechat_base_info_redirect_url = window.location.href
                    cookies.set('wechat_base_info_redirect_url', wechat_base_info_redirect_url)
                    window.location.href = config.api.serviceUrl + '/wechat/user/login/base'
                }
            }
        })
    }

    aLiOrderPay = (orderId, type) => {

        let successFun = function () { }
        if (type === 'recharge') {
            successFun = api.alipayRecharge
        } else {
            successFun = api.alipayPay
        }
        successFun({
            order_id: orderId
        }, {
            success: (data) => {
                this.setState({
                    payHtml: data
                }, () => {
                    document.forms['alipay_submit'].submit();
                })
            }
        })
    }

    otherOrderPay = orderId => {
        api.payOrder({
            order_id: orderId
        }, {
            success: (data) => {
                Toast.success('支付成功', 1, () => {
                    this.props.history.push('/pay/success')
                })
            }
        })
    }
}
export default withRouter(PayOrder)